/* 
 * File:   Library.cpp
 * Author: Lucky
 * 
 * Created on 22. září 2011, 10:33
 */

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <fstream>
#include "Library.h"
#include "Field.h"

using namespace std;

Library::Library() {
}

Library::Library(const Library& orig) {
}

Library::~Library() {
}

int Library::LoadInt(const char* string, int min, int max) {
    int number;
    bool filled = false;
    do {
        if (filled) {
            std::cout << "Spatne zadani!" << std::endl;
            if (!std::cin.good()) {
                std::cin.clear();
                std::cin.ignore(INT_MAX, '\n');
            }
        }
        std::cout << string;
        std::cin >> number;
        std::cin.ignore(INT_MAX, '\n');
        filled = true;
    } while (!std::cin.good() || number < min || number > max);
    return number;
}

int* Library::LoadCoordinates(int count, int m, int n) {
    ///Coordinate** res = new Coordinate*[count];
    int* res = new int[2 * count];

    for (int i = 0; i < (2 * count); i += 2) {
        std::stringstream stream1;
        stream1 << "Souradnice " << (i + 2) / 2 << ", X osa: ";
        std::string str(stream1.str());
        int x = Library::LoadInt(str.c_str(), 0, m - 1);

        std::stringstream stream2;
        stream2 << "Souradnice " << (i + 2) / 2 << ", Y osa: ";
        str = std::string(stream2.str());
        int y = Library::LoadInt(str.c_str(), 0, n - 1);
        res[i] = x;
        res[i + 1] = y;
    }
    return res;
}

void Library::getNextCoordinate(int x, int y, int width, int * nextX, int * nextY) {
    //  cout << "Rx ry "<<a<<" "<<b << endl;
    if (x + 1 >= width) {
        x = 0;
        ++y;
    } else {
        ++x;
    }
    *nextX = x;
    *nextY = y;
}

int Library::getCoordLength(int width, int x, int y) {
    return y * width + x;

}

 int*  Library::LoadDataFromFile(string fileName,int * width, int * height, int *q ){
  string line; //this will contain the data read from the file
  ifstream myfile ("data.txt"); //opening the file.
   int* res;
   
  if (myfile.is_open()) //if the file is open
  {
      int charPos;
      
      //nacteni sirky
      getline (myfile,line);     
      charPos=line.find(':');
      istringstream isa(line.substr(charPos+1));
      isa>>*width;
      //nacteni vysky
      getline (myfile,line);
        charPos=line.find(':');
        istringstream isb(line.substr(charPos+1));
        isb>>*height;
       //nacteni pocetSouradnic
      getline (myfile,line);     
        charPos=line.find(':');
        istringstream isc(line.substr(charPos+1));        
        isc>>*q;     
      cout <<"Sirka "<<*width<<" vyska: "<<*height<<" pocet souradnic: "<<*q<<endl;
        //vytvoreni pole
         res = new int[ 2*(*q)];
        cout<<2*(*q)<<endl;
        getline (myfile,line);
        int counter=0,x,y;
        for(int i=0;i<=line.size()&&i<=3*(*q);i++){
            char c = line[i];
            char a[1];
            // cout <<"Pruchod "<<endl;
            if(c=='x'){
               // cout <<"Nalezeno "<<endl;
//                istringstream ix((line[i-1]));
//                ix>>x;
                a[0]=line[i-1];
                res[counter]=atoi(a);                  
//                 istringstream iy((line[i+1]));
//                iy>>y;
                counter++;
                a[0]=line[i+1];
                res[counter]=atoi(a);
                counter++;                
             // cout << "nalezeny sour " <<line[i-1]<<" ulozeny "<< res[counter-2]<<" "<<line[i+1]<<" ulozeny "<< res[counter-1] << endl;
                
            }
        }
        
        cout<<"Velikost pole "<<counter<<endl;
       
    
  }
  myfile.close();
  return res;
 }

//void Library::DestroyCoordinates(Coordinate** coords, int count) {
//    for (int i = 0; i < count; i++) {
//        delete coords[i];
//    }
//    delete coords;
//}

int Library::getXCoord(int width, int fieldNumber) {
    return fieldNumber % width;
}

int Library::getYCoord(int width, int fieldNumber) {
    return fieldNumber / width;
}
